// pages/addressList/index.js
import {getSetting,chooseAddress,openSetting} from '../../utils/asyncWx.js'
import regeneratorRuntime from '../../lib/runtime/runtime';
const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: []
  },
  onShow () {
    this.getAddressList()
  },
  //获取地址列表
  getAddressList(){
    App._get('User/Api001', {}, (res) => {
      let addressList = res.items
      this.setData({addressList})
    })
  },
  //设置默认
  radioChange(e){
    let id=e.currentTarget.dataset.id
    App._post_form('User/Api005', {id}, (res) => {
      this.getAddressList()
      wx.removeStorageSync('address')
      this.onNavigateBack()
    })
  },
  //删除地址
  removeAddress(e) {
    let _this = this,
      id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "您确定要移除当前收货地址吗?",
      success(o) {
        o.confirm && App._post_form('User/Api006', {id}, (result)=>{
          _this.getAddressList();
        });
      }
    });
  },
  async chooseAddress() {
    try {
      // 1.获取权限状态
      const res1 = await getSetting();
      const scopeAddress = res1.authSetting["scope.address"];
      // 2.判断权限状态
      if (scopeAddress === false) {
        // 用户拒绝了授权 先诱导用户打开授权
        await openSetting();
      }
      // 3. 调用获取收货地址
      let address = await chooseAddress();
      address.all=address.provinceName+address.cityName+address.countyName+address.detailInfo
      // 4.本地存储数据
      wx.setStorageSync("address", address);
      this.onNavigateBack()
    } catch (error) {
      console.log(error);
    }
  },
  //跳转回原页面
  onNavigateBack() {
    wx.navigateBack();
  },
})